﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using Cronus.View.Data;

namespace Cronus.Windows
{
    /// <summary>
    /// Interaction logic for EditTask.xaml
    /// </summary>
    public partial class EditTask : Window
    {
        //*** Constants ***//
        //*** Static ***//
        //*** Member Variables ***//
        private CronusData.TasksRow _CurrentTask;
        private CronusData _CronusDataSet;

        //*** Properties ***//
        public CronusData CronusDataSet
        {
            get { return _CronusDataSet; }
            set
            {
                // Bind the ComboBox to the tasks table.
                _CronusDataSet = value;
                if (null != _CronusDataSet)
                {
                    _TaskComboBox.ItemsSource = _CronusDataSet.Tasks.ViewByTaskName;
                    if (0 < _CronusDataSet.Tasks.Rows.Count)
                    {
                        _TaskComboBox.SelectedIndex = 0;
                    }
                }
            }
                
        }

        //*** Constructors ***//
        public EditTask()
        {
            InitializeComponent();
            CronusDataSet = null;
            _CurrentTask = null;
            _UpdateBtn.IsEnabled = false;
        }

        //*** Methods ***//
        /// <summary>
        /// This function will save any changes the user has made to the
        /// current task.
        /// </summary>
        /// <param name="sender">Not used.</param>
        /// <param name="e">Not used.</param>
        private void UpdateBtn_Click(object sender, RoutedEventArgs e)
        {
            string strNewName = _NewTaskName.Text;
            if (String.Empty != strNewName)
            {
                // As long as the string isn't empty, update it.
                CronusDataSet.UpdateTaskName(_CurrentTask, strNewName);
                _UpdateBtn.IsEnabled = false;
            }
        }

        /// <summary>
        /// This function will allow the user to delete the currently
        /// selected task.
        /// </summary>
        /// <param name="sender">Not used.</param>
        /// <param name="e">Not used.</param>
        private void DeleteBtn_Click(object sender, RoutedEventArgs e)
        {
            // Prompt the user to make sure they want to delete the task.
            if (MessageBoxResult.Yes == MessageBox.Show(Cronus.View.Properties.Resources.DeleteTaskPrompt,
                                                        Cronus.View.Properties.Resources.Title,
                                                        MessageBoxButton.YesNo))
            {
                // Delete the task.
                CronusDataSet.DeleteTask(_CurrentTask);
                _CurrentTask = null;
                if (0 < _TaskComboBox.Items.Count)
                {
                    _TaskComboBox.SelectedIndex = 0;
                }
            }
        }

        /// <summary>
        /// This function will update the current statistics shown to the user.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TaskComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // Local variables
            DataRowView aView = null;
            CronusData.TasksRow aTask = null;
            TextBlock aBlock = null;
            TaskSummary aSummary = null, aTotalSummary = null;

            // Clear out the current list of items in the StackPanel.
            _TaskStackPanel.Children.Clear();

            // Get the currently selected item from the ComboBox.
            aView = _TaskComboBox.SelectedItem as DataRowView;
            if (null != aView)
            {
                aTask = aView.Row as CronusData.TasksRow;
                if (null != aTask)
                {
                    // Save the pointer to the task for later.
                    _CurrentTask = aTask;

                    // Fill the StackPanel with new information.
                    aBlock = new TextBlock();
                    _TaskStackPanel.Children.Add(new TextBlock());

                    aSummary = aTask.Summary;
                    aTotalSummary = CronusDataSet.GetTotalSummary();

                    AddSummaryToTaskOverview("Today", aSummary.GetHoursToday(), aTotalSummary.GetHoursToday());
                    AddSummaryToTaskOverview("This Week", aSummary.GetHoursInWeek(), aTotalSummary.GetHoursInWeek());
                    AddSummaryToTaskOverview("This Month", aSummary.GetHoursInMonth(), aTotalSummary.GetHoursInMonth());
                    AddSummaryToTaskOverview("Total Hours", aSummary.GetTotalHours(), aTotalSummary.GetTotalHours());

                    // Add the name of the task to the text box
                    // in case users want to change it.
                    _NewTaskName.Text = aTask.Name;
                    _NewTaskName.Focus();
                    _UpdateBtn.IsEnabled = false;
                }
            }
        }

        /// <summary>
        /// This function will format the given parameters and put them into the
        /// stackpanel that shows a summary of the currently selected task.
        /// </summary>
        /// <param name="strHeader">Header of the text.</param>
        /// <param name="dHours">Hours worked on the task in a time period.</param>
        /// <param name="dTotalHours">Total hours worked in the same time period.</param>
        private void AddSummaryToTaskOverview(string strHeader, double dHours, double dTotalHours)
        {
            // Local variables
            TextBlock aBlock = new TextBlock();
            double dPercent = 0;
            if (0 < dTotalHours)
            {
                dPercent = (dHours / dTotalHours) * 100;
            }

            aBlock.Text = strHeader + ": ";
            aBlock.Text += TaskSummary.FormatHoursAsString(dHours);
            aBlock.Text += " out of " + TaskSummary.FormatHoursAsString(dTotalHours) + " hours";
            aBlock.Text += " (" + dPercent.ToString("F1") + "%)";
            _TaskStackPanel.Children.Add(aBlock);
        }

        /// <summary>
        /// This is a callback when the text in the 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void NewTaskName_TextChanged(object sender, TextChangedEventArgs e)
        {
            // The user has changed the text in some way, but only enable
            // the button if there is a different name.
            if ((0 < _NewTaskName.Text.Length) &&
                (_NewTaskName.Text != _CurrentTask.Name))
            {
                _UpdateBtn.IsEnabled = true;
            }
            else
            {
                _UpdateBtn.IsEnabled = false;
            }
        }

        /// <summary>
        /// This function is called as the window is activated.  It will do
        /// some startup work to make sure the window is properly setup for
        /// the user.
        /// </summary>
        /// <param name="sender">Not used.</param>
        /// <param name="e">Not used.</param>
        private void Window_Activated(object sender, EventArgs e)
        {
            // Only enable the delete button if we're not currently timing
            // a task.
            _DeleteBtn.IsEnabled = true;
            if (_CronusDataSet.IsTiming)
            {
                _DeleteBtn.IsEnabled = false;
            }
        }
    }
}
